import { request } from 'umi';
import { SUCCESS_CODE } from '@/utils/typings.d';
import type { CreateWebNavType, UpdateWebNavType, WebNavTableListItem } from './typings.d';

export async function queryWebNavList(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: WebNavTableListItem[];
}> {
  const res = await request('/api/web-nav/v1.0.0/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function createWebNav(params: CreateWebNavType) {
  const res = await request('/api/web-nav/v1.0.0/webNav', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) return { data: res.data, success: true, message: '添加成功' };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}
export async function removeWebNav(id: string) {
  const res = await request(`/api/web-nav/v1.0.0/webNav/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function updateWebNav(id: string, newInfo: UpdateWebNavType) {
  if (!id || !newInfo || Object.keys(newInfo).length === 0) {
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  }
  const res = await request(`/api/web-nav/v1.0.0/webNav/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function getWebNavCardList(): Promise<{
  success: boolean;
  data: WebNavTableListItem[];
  message: string;
}> {
  const res = await request('/api/web-nav/v1.0.0/webnav/cardlist');
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '获取成功' };
  return {
    success: false,
    data: res?.data || [],
    message: res?.message || '获取失败',
  };
}
